import time
from selenium import webdriver
from selenium.webdriver.support.select import Select


class shopping:
    def __init__(self):
        wd=webdriver.Firefox()
        wd.maximize_window()
        self.lh='http://192.168.0.118'
        wd.get(self.lh + '/TinyShop/index.php')
        wd.implicitly_wait(3)
        self.wd=wd

    def QT_login(self,user,pwd):
        wd=self.wd
        wd.find_element_by_link_text("登录").click()
        wd.find_element_by_id("account").clear()
        wd.find_element_by_id("account").click()
        wd.find_element_by_id("account").send_keys(user)
        wd.find_element_by_name("password").clear()
        wd.find_element_by_name("password").click()
        wd.find_element_by_name("password").send_keys(pwd)
        wd.find_element_by_xpath("//*[@id=\"main\"]//div/div/form/ul/li[4]/button").click()


    def QT_register(self,li):
        wd=self.wd
        wd.find_element_by_link_text("注册").click()
        wd.find_element_by_id("email").send_keys(li[0])
        wd.find_element_by_name("password").send_keys(li[1])
        wd.find_element_by_name("repassword").send_keys(li[2])
        wd.find_element_by_id("verifyCode").send_keys("aaaa")
        wd.find_element_by_id("readme").click()
        wd.find_element_by_xpath("/html/body/div[3]/div[1]/div/form/ul/li[6]/button").click()
        return wd

    def QT_buy(self,li):
        wd=self.wd
        wd.find_element_by_link_text("服装").click()
        wd.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div[2]/dl/dd/ul/li[1]/dl/dt").click()
        wd.find_element_by_id("add-cart").click()
        #选择商品
        wd.find_element_by_xpath("//*[@id=\"product-intro\"]/div[3]/div/dl[1]/dd/ul/li[1]/span").click()
        wd.find_element_by_xpath("//*[@id=\"product-intro\"]/div[3]/div/dl[2]/dd/ul/li[1]/span").click()
        wd.find_element_by_id("buy-now").click()
        #确认订单信息
        wd.find_element_by_link_text("立即结算").click()
        wd.find_element_by_id("address_other").click()
        #选择支付页面
        wd.switch_to.frame(wd.find_element_by_tag_name("iframe"))#切换至添加地址页面
        p=wd.find_element_by_id("province")#选择城市
        Select(p).select_by_value(li[0])
        c=wd.find_element_by_id("city")
        Select(c).select_by_value(li[1])
        b=wd.find_element_by_id("county")
        Select(b).select_by_value(li[2])
        wd.find_element_by_name("zip").send_keys(li[3])#邮政编码
        wd.find_element_by_name("addr").send_keys(li[4])#街道地址
        wd.find_element_by_name("accept_name").send_keys(li[5])#收货人姓名
        wd.find_element_by_name("mobile").send_keys(li[6])
        wd.find_element_by_xpath("/html/body/div[2]/form/table/tbody/tr[8]/td/input").click()

        lo=wd.switch_to.default_content()#切换至支付页面
        if lo==True:
            wd.find_element_by_xpath("/html/body/div[3]/div[2]/div/form/div[6]/p/input").click()

            wd.find_element_by_xpath("//*[@id=\"main\"]/div[2]/form/div[3]/p").click()
            return wd
        else:
            return wd

    def __del__(self):
        self.wd.quit()




if __name__ == "__main__":
    s=shopping()
    s.QT_login("123@qq.com","123456")
    s.QT_register("1234@qq.com","123456","123456",)
    s.QT_buy("四川省","成都市","锦江区","614400","hahaha","long","15308196404")
